package cn.yunhe.spring.struts.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.yunhe.spring.mybatis.model.Page;
import cn.yunhe.spring.mybatis.model.User;
import cn.yunhe.spring.mybatis.service.IUserService;

public class UserAction extends ActionSupport implements ModelDriven<Object>{
	
	private static final long serialVersionUID = 1L;

	@Autowired
	private IUserService userService;
	
	private User user;
	private int pageSize;
	private int pageNum;
	private Page page;

	public void setUser(User user) {
		this.user = user;
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public String findUser(){
		user = userService.getUser(user);
        ServletActionContext.getRequest().setAttribute("data", user);
		return "success";
	}
	
	public String addUser(){
		userService.addUser(user);
		return "success";
	}
	
	public String delUser(){
		userService.delUser(user);
		return "success";
	}
	
	public String editUser(){
		userService.editUser(user);
		return "success";
	}
	
	public String queryLikeUser(){
		Map<String,Object> cond=new HashMap<String,Object>();
		cond.put("pageSize", pageSize);
		cond.put("pageNum", pageNum);
		cond.put("user", user);
		page = userService.queryLikeUser(cond);
		return "success";
	}

	@Override
	public User getModel() {
		if(user==null){
			user = new User();
		}
		return user;
	}
	
}
